package com.yihaodian.index.solr;

import java.io.InputStream;

import org.apache.log4j.Logger;
import org.apache.solr.core.SolrConfig;
import org.apache.solr.schema.IndexSchema;
import org.xml.sax.InputSource;

import com.yihaodian.common.Files;

public class SolrConfigs {
	private static final Logger log = Logger.getLogger(SolrConfigs.class);

	String instanceDir;

	String name;

	String configXml;

	String schemaXml;

	transient SolrConfig solrConfig;

	transient IndexSchema indexSchema;

	public SolrConfigs() {

	}

	public SolrConfigs(String instanceDir, String name, SolrConfig solrConfig,
			IndexSchema indexSchema) {
		this.instanceDir = instanceDir;
		this.name = name;
		this.indexSchema = indexSchema;
		this.solrConfig = solrConfig;
	}

	public String getInstanceDir() {
		return instanceDir;
	}

	public void setInstanceDir(String instanceDir) {
		this.instanceDir = instanceDir;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getConfigXml() {
		return configXml;
	}

	public void setConfigXml(String configXml) {
		this.configXml = configXml;
	}

	public String getSchemaXml() {
		return schemaXml;
	}

	public void setSchemaXml(String schemaXml) {
		this.schemaXml = schemaXml;
	}

	public SolrConfig getSolrConfig() {
		if (solrConfig == null && configXml != null) {
			try {
				InputSource is = new InputSource(
						Files.strAsInputStream(configXml));
				solrConfig = new SolrConfig(instanceDir, name, is);
			} catch (Exception e) {
				log.error(e, e);
			}
		}

		return solrConfig;
	}

	public void setSolrConfig(SolrConfig solrConfig) {
		this.solrConfig = solrConfig;
	}

	public IndexSchema getIndexSchema() {
		if (indexSchema == null && schemaXml != null) {
			try {
				SolrConfig sc = this.getSolrConfig();
				InputSource is = new InputSource(
						Files.strAsInputStream(schemaXml));
				indexSchema = new IndexSchema(sc, name, is);
			} catch (Exception e) {
				log.error(e, e);
			}
		}

		return indexSchema;
	}

	public void setIndexSchema(IndexSchema indexSchema) {
		this.indexSchema = indexSchema;
	}

	public static SolrConfigs loadFromClassPath(String instanceDir, String name) {
		try {
			log.info("load solrConfig from classPath " + name);

			InputStream is = getResourceAsStream(name + "_" + "solrconfig.xml");
			InputStream is2 = getResourceAsStream(name + "_" + "solrconfig.xml");
			SolrConfig solrConfig = new SolrConfig(instanceDir, name,
					new InputSource(is));
			String configXml = Files.readAsString(is2);

			InputStream iis = getResourceAsStream(name + "_" + "solrschema.xml");
			InputStream iis2 = getResourceAsStream(name + "_"
					+ "solrschema.xml");
			IndexSchema indexSchema = new IndexSchema(solrConfig, name,
					new InputSource(iis));
			String schemaXml = Files.readAsString(iis2);

			SolrConfigs configs = new SolrConfigs(instanceDir, name,
					solrConfig, indexSchema);
			configs.setConfigXml(configXml);
			configs.setSchemaXml(schemaXml);

			log.info("load solrConfig from classPath end " + name);
			return configs;
		} catch (Exception e) {
			log.error(e, e);
		}

		return null;
	}

	static InputStream getResourceAsStream(String name) {
		return SolrConfigs.class.getClassLoader().getResourceAsStream(name);
	}
}
